package com.panshi.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.panshi.service.MatterService;
import com.panshi.tools.DataResult;
import com.panshi.tools.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "问题业务接口")
@RestController
@Slf4j
public class MatterController {

    @Autowired
    private MatterService matterService;

    @ApiOperation("查询热榜问题接口")
    @GetMapping("matter/queryHot") // http://localhost:1001/matter/queryHot?pageNumber=1&pageSize=10
    @HystrixCommand(fallbackMethod = "queryGet")
    public DataResult queryMatterHot(PageBean pageBean){
        DataResult result =  matterService.queryMatterHot(pageBean);
        if(result.getData() == null){
            throw new RuntimeException();
        }

        return result;
    }

    @ApiOperation("查询已回答问题接口")
    @GetMapping("matter/answered") // http://localhost:1001/matter/answered?pageNumber=1&pageSize=10
    @HystrixCommand(fallbackMethod = "queryGet")
    public DataResult queryAnswered( PageBean pageBean){
        DataResult result = matterService.queryAnswered(null,pageBean);
        if(result.getData() == null){
            throw new RuntimeException();
        }
        return result;
    }

    @ApiOperation("根据分类id查询已回答问题接口")
    @ApiImplicitParam(name = "classifyId", value = "分类id", required = true)
    @GetMapping("matter/answeredCid/{classifyId}") // http://localhost:1001/matter/answeredCid/2?pageNumber=1&pageSize=10
    @HystrixCommand(fallbackMethod = "queryParamGet")
    public DataResult queryAnswered(@PathVariable Integer classifyId, PageBean pageBean){
        DataResult result = matterService.queryAnswered(classifyId,pageBean);
        if(result.getData() == null){
            throw new RuntimeException();
        }
        return result;
    }

    @ApiOperation("查询未回答问题接口")
    @GetMapping("matter/unanswered") // http://localhost:1001/matter/unanswered?pageNumber=1&pageSize=10
    @HystrixCommand(fallbackMethod = "queryGet")
    public DataResult queryUnanswered(PageBean pageBean){
        DataResult result = matterService.queryUnanswered(null,pageBean);
        if(result.getData() == null){
            throw new RuntimeException();
        }
        return result;
    }

    @ApiOperation("根据分类id查询未回答问题接口")
    @ApiImplicitParam(name = "classifyId", value = "分类id", required = true)
    @GetMapping("matter/unansweredCid/{classifyId}") // http://localhost:1001/matter/unansweredCid/2?pageNumber=1&pageSize=10
    @HystrixCommand(fallbackMethod = "queryParamGet")
    public DataResult queryUnanswered(@PathVariable Integer classifyId, PageBean pageBean){
        DataResult result = matterService.queryUnanswered(classifyId,pageBean);
        if(result.getData() == null){
            throw new RuntimeException();
        }
        return result;
    }

    @ApiOperation("查询推荐问题接口")
    @GetMapping("matter/recommend") // http://localhost:1001/matter/recommend?pageNumber=1&pageSize=10
    @HystrixCommand(fallbackMethod = "queryGet")
    public DataResult queryRecommend(PageBean pageBean){
        DataResult result = matterService.queryRecommend(null,pageBean);
        if(result.getData() == null){
            throw new RuntimeException();
        }
        return result;
    }

    @ApiOperation("根据分类id查询推荐问题接口")
    @GetMapping("matter/recommendCid/{classifyId}") // http://localhost:1001/matter/recommendCid/2?pageNumber=1&pageSize=10
    @HystrixCommand(fallbackMethod = "queryParamGet")
    public DataResult queryRecommendByCid(@PathVariable Integer classifyId, PageBean pageBean){
        DataResult result = matterService.queryRecommend(classifyId,pageBean);
        if(result.getData() == null){
            throw new RuntimeException();
        }
        return result;
    }

    @ApiOperation("根据问题id查询")
    @ApiImplicitParam(name = "matterId", value = "问题id", required = true)
    @GetMapping("matter/info/{matterId}") // http://localhost:1001/matter/info/2
    @HystrixCommand(fallbackMethod = "queryInfoGet")
    public DataResult queryMatterInfo(@PathVariable Integer matterId){
        DataResult result = matterService.queryMatterInfo(matterId);
        return result;
    }

    @ApiOperation("增加阅读数量")
    @ApiImplicitParam(name = "matterId", value = "问题id", required = true)
    @GetMapping("matter/read/{matterId}")
    public void updateReadCount(@PathVariable Integer matterId){
        matterService.updateCount(matterId,"read");
    }


    @ApiOperation("改变回答数量")
    @GetMapping("matter/answer/{matterId}/{code}")
    public void updateAnswerCount(@PathVariable Integer matterId,@PathVariable String code){
        matterService.updateCount(matterId,code);
    }

    // 熔断方法
    public DataResult queryInfoGet(@PathVariable Integer matterId){
        DataResult result = new DataResult();
        result.setMsg("matterId -> "+ matterId +"，请求超时，或者没有对应的数据，请重试~MatterController");
        return result;
    }

    // 熔断方法
    public DataResult queryGet(PageBean pageBean){
        return this.respResult(null);
    }
    // 熔断方法
    public DataResult queryParamGet(@PathVariable Integer classifyId,PageBean pageBean){
        return this.respResult(classifyId);
    }
    // 响应失败提示
    private DataResult respResult(Integer classifyId){
        DataResult result = new DataResult();
        result.setCode(500);
        if(null == classifyId){
            result.setMsg("请求超时，请重试~MatterController");
            return result;
        }
        result.setMsg("classifyId -> "+ classifyId +"，请求超时，或者没有对应的数据，请重试~MatterController");
        return result;
    }



}

